/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "interface/network_adapter/cooling.h"

namespace dev {

bool NetworkAdapter_Cooling::update_chip_temp_by_ncsi() {
    uint16_t chip_temp = 0;
    bool     ret = m_ncsi_over_mctp_wx->get_temperature(DEFAULT_PACKAGE_ID, WX_GET_TEMP_CHANNEL_ID,
                                                        &chip_temp);
    if (!ret) {
        elog("Error: Failed to update chip temp");
        return false;
    }
    TemperatureCelsius.set_value(static_cast<int16_t>(chip_temp));
    TemperatureStatus.set_value(chip_temp < CHIP_TEMP_INVALID ? 0 : 1);
    return true;
}

// bool NetworkAdapter_Cooling::update_max_sfp_temperature(uint16_t max_temperature, uint8_t status)
// {
//     if ((max_temperature & OM_TEMP_SIGN_BIT) == 0) {
//         MaxSFPTemperatureCelsius.set_value(max_temperature & OM_TEMP_DATA_BIT);
//     } else {
//         MaxSFPTemperatureCelsius.set_value(-1 * (max_temperature & OM_TEMP_DATA_BIT));
//     }
//     MaxSFPTemperatureStatus.set_value(status);
//     return true;
// }

void NetworkAdapter_Cooling::start_ncsi_update_task_wx(ncsi_over_mctp_wx_ptr ncsi_over_mctp,
                                                       mc::milliseconds      interval) {
    m_ncsi_timer        = new mc::core::timer(this);
    m_ncsi_over_mctp_wx = ncsi_over_mctp;
    m_ncsi_timer->timeout.connect([this]() {
        update_chip_temp_by_ncsi();
    });
    m_ncsi_timer->set_single_shot(false);
    m_ncsi_timer->start(interval);
}

void NetworkAdapter_Cooling::stop_ncsi_update_task() {
    m_ncsi_timer->stop();
}

} // namespace dev

MC_REFLECT(dev::NetworkAdapter_Cooling)
